import axo from "@/assets/js/axo";

 // vuex理念中 actions -> dispatch
 // 在这里提交mutation，并导出这个
export const showMsg = ({ commit }, arg1) => {
    // commit({
    //    type:"showMsg", // 这个type很重要，vue会自动去找mutations.js中名为showMsg的方法
    //    arg1, // 成功后把得到的数据通过commit传入mutations
    // });
    commit("getMsg", arg1);
};

/**
 * 更改用户是否登录状态
 * @param  {[type]} commit [description]
 * @param  {[type]} status [true/false]
 * @return {[type]}        [description]
 */
export const loginState = ({commit}, status) => {
    commit("checkIsLogin", status);
};

/**
 * 保存用户权限
 * @param  {[type]} commit [description]
 * @param  {[type]} status [description]
 * @return {[type]}        [description]
 */
export const keepUserAuth = ({commit}, data) => {
    commit("keepUserAuth", data);
};

/**
 * 保存用户资料
 * @param  {[type]} commit [description]
 * @param  {[type]} status [description]
 * @return {[type]}        [description]
 */
export const keepSysUser = ({commit}, data) => {
    commit("keepSysUser", data);
};

/**
 * 保存用户会话tokenId
 * @param  {[type]} commit [description]
 * @param  {[type]} status [description]
 * @return {[type]}        [description]
 */
export const keepTokenId = ({commit}, data) => {
    commit("keepTokenId", data);
};

export const getRSAPublicKey = ({commit}, params) => {
    // console.log("getRSAPublicKey");
    if(!sessionStorage.getItem("RSAPublicKey")){
        return new Promise((resolve, reject) => {
            axo({
                method: 'post',
                url: "/data/key/public"
            }).then(response => {
                let key = response.data.public_key;
                sessionStorage.setItem("RSAPublicKey", key);
                commit("getRSAPublicKey", key);
                resolve(response);
            }).catch((error) => {
                reject(error);
            });
        });
    }else{
        commit("getRSAPublicKey", sessionStorage.getItem("RSAPublicKey"));
    }
};

export const getDESPublicKey = ({commit}, params) => {
    // console.log("getDESPublicKey");
    if(!sessionStorage.getItem("DESPublicKey")){
        return new Promise((resolve, reject) => {
            axo({
                method: 'post',
                url: "/data/des/key"
            }).then(response => {
                let key = response.data.des_key;
                sessionStorage.setItem("DESPublicKey", key);
                commit("getDESPublicKey", key);
                resolve(response);
            }).catch((error) => {
                reject(error);
            });
        });
    }else{
        commit("getDESPublicKey", sessionStorage.getItem("DESPublicKey"));
    }
};
